package jp.heartdokyun.activity;

import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.listener.GoBackListener;
import jp.heartdokyun.listener.TopBackListener;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class GameFailActivity extends Activity {

	/** Called when the activity is first created. */
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private MediaPlayer mMediaPlayer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSounds();
		setContentView(R.layout.game_fail);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		TextView tvTitle = (TextView) findViewById(R.id.tv_game_fail_title);
		// tvTitle.setText(getIntent().getStringExtra("CHALLENGE_TYPE"));

		String Level = getIntent().getStringExtra("LEVEL");
		// String Type = getIntent().getStringExtra("CHALLENGE_TYPE_VALUE");
		if (Level.equals("")) {
			Level = "1";
		}
		String Title = getResources().getString(R.string.game_tilte) + " "
				+ getIntent().getStringExtra("CHALLENGE_TYPE") + " Lv." + Level;
		tvTitle.setText(Title);

		ImageButton btnReChallenge = (ImageButton) findViewById(R.id.btn_re_challenge);
		btnReChallenge.setOnClickListener(new GoBackListener(this));
		ImageButton btnStageSelection = (ImageButton) findViewById(R.id.btn_stage_selection);

		btnStageSelection.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itStageSelection = new Intent(GameFailActivity.this,
						StageSelectionActivity.class);
				itStageSelection.putExtra("CHALLENGE_TYPE", getIntent()
						.getStringExtra("CHALLENGE_TYPE"));
				startActivity(itStageSelection);
			}
		});
		ImageButton ibBack = (ImageButton) findViewById(R.id.ib_return_menu);
		ibBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itMenu = new Intent(GameFailActivity.this,
						MenuActivity.class);

				startActivity(itMenu);
			}
		});

		ImageButton ibCardCollection = (ImageButton) findViewById(R.id.ib_main_card_collection);
		ibCardCollection.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent itMenu = new Intent(GameFailActivity.this,
						CardCollectionActivity.class);
				startActivity(itMenu);
			}
		});

		ImageButton ibo = (ImageButton) findViewById(R.id.ic_o);
		ibo.setOnClickListener(new TopBackListener(this));

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				playSound(1, 0);
			}
		}).run();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				playSound(2, 0);
			}
		}).run();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				playSound(3, 0);
			}
		}).run();

		new Thread() {
			@Override
			public void run() {
				try {
					sleep(500);
					ImageView ivBreakHeart = (ImageView) findViewById(R.id.iv_gf_break_heart);
					AnimationDrawable frameAnimation = (AnimationDrawable) ivBreakHeart
							.getBackground();
					frameAnimation.setOneShot(true);
					frameAnimation.start();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}.start();

	}

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1,
				mSoundPool.load(getApplicationContext(), R.raw.zannen, 1));
		soundPoolMap
				.put(2, mSoundPool.load(getApplicationContext(),
						R.raw.matashiyoune, 1));
		soundPoolMap
				.put(3, mSoundPool.load(getApplicationContext(),
						R.raw.mataasondene, 1));
	}

	@Override
	public void onDestroy() {
		mSoundPool.release();
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onDestroy();
	}

	@Override
	public void onResume() {
		// if (MainActivity.isEnableBGM) {
		// mMediaPlayer.start();
		// } else {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		// }

		if (MainActivity.isEnableBGM) {
			mMediaPlayer = MediaPlayer.create(getApplicationContext(),
					R.raw.other_bgm);
			float volume = Utils.getBGMVolume(getApplicationContext());
			mMediaPlayer.setVolume(volume, volume);
			mMediaPlayer.setLooping(true);
			mMediaPlayer.start();
		}
		super.onResume();
	}

	@Override
	public void onPause() {
		// if (mMediaPlayer.isPlaying()) {
		// mMediaPlayer.pause();
		// mMediaPlayer.seekTo(0);
		// }
		if (mMediaPlayer != null)
			mMediaPlayer.release();
		super.onPause();
	}

	public void playSound(int sound, int loop) {
		float volume = Utils.getVoiceVolume(getApplicationContext());
		mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
	}
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}
}